#include "CommunicationsHandler.h"

extern uint8 u8Comm_RxSMACPacket[COMM_SMAC_RX_PACKET_SIZE];
extern uint8 u8Comm_TxSMACPacket[COMM_SMAC_TX_PACKET_SIZE];

extern uint8 u8Comm_RxSCIPacket[COMM_SCI_RX_PACKET_SIZE];
extern uint8 u8Comm_TxSCIPacket[COMM_SCI_TX_PACKET_SIZE];

extern uint8 u8Comm_SCIPending;

void vfnComm_BuildSCIPacket (void)
{
    uint8 u8counter;
    for(u8counter = 0; u8counter < COMM_SCI_TX_PACKET_SIZE; u8counter++)
    {
        u8Comm_TxSCIPacket[u8counter] = u8Comm_RxSMACPacket[u8counter + 3];
    }
}

void vfnComm_BuildSMACPacket (void)
{
    u8Comm_TxSMACPacket[0] = COMM_MAC_ADDR_HIGH;
    u8Comm_TxSMACPacket[1] = COMM_MAC_ADDR_LOW;
    
    u8Comm_TxSMACPacket[2] = 0;  // Message number
  
    
}

void vfnComm_HandleSCIRx (void)
{
    uint8 u8checksum = 0;
    uint8 u8counter;
            
    for (u8counter = 0; u8counter < COMM_SCI_RX_PACKET_SIZE; u8counter++)
    {
        while(!u8SCI1_RxPending());
        u8Comm_RxSCIPacket[u8counter] = u8SCI1_ReadByte();
        u8checksum ^= u8Comm_RxSCIPacket[u8counter];               
    }
    
    vfnSCI1_SendByte(u8checksum);
    while(!u8SCI1_TxBufferEmpty());

    u8Comm_SCIPending = 0; 
    
    vfnSCI1_EnableInterrupts();     
}

void vfnComm_HandleSCITx (void)
{
    uint8 u8counter;
    uint8 u8checksum = 0;
            
    for (u8counter = 0; u8counter < COMM_SCI_TX_PACKET_SIZE; u8counter++)
    {
        u8checksum ^= u8Comm_TxSCIPacket[u8counter];
        vfnSCI1_SendByte(u8Comm_TxSCIPacket[u8counter]);
        while(!u8SCI1_TxBufferEmpty());    
    }
    
    vfnSCI1_SendByte(u8checksum);
    while(!u8SCI1_TxBufferEmpty());

    u8Comm_SCIPending = 0;
    
    vfnSCI1_EnableInterrupts();     
}











